package textures.viewer;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import textures.BitmapLoader;
import textures.TextureReader;

public class JPanelViewImage extends JPanel {
	
	private static final long serialVersionUID = 1L;
	
	private ImageIcon image;
	
	private String path;
	
	public JPanelViewImage(String imagePath){
		try {
			BufferedImage bufferedImage;
			if (imagePath.endsWith(".bmp")) {
				System.out.println("otro");
			    bufferedImage = BitmapLoader.loadBitmap(imagePath);
			} else {
				System.out.println("imgen");
			    bufferedImage = TextureReader.readImage(imagePath);
			}
			path = imagePath;
			image = new ImageIcon(bufferedImage);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	public JPanelViewImage(){
		image = null; 
		path = null;
	}
	
	public ImageIcon getImage() {
		return image;
	}

	public void setImage(ImageIcon image) {
		this.image = image;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	
	public void createImage(String imagePath){
		try {
			BufferedImage bufferedImage;
			if (imagePath.endsWith(".bmp"))
			    bufferedImage = BitmapLoader.loadBitmap(imagePath);
			else
			    bufferedImage = TextureReader.readImage(imagePath);
			path = imagePath;
			image = new ImageIcon(bufferedImage);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}	
	
    @Override
     public void paint(Graphics g) {
    	if(image != null)
    		g.drawImage(image.getImage(), 0, 0, getWidth(), getHeight(),this);    
        setOpaque(false);
        super.paint(g);
    }

	
}
